<?php
include_once(JL_ROOT.'/jl_controller.cls.php');
include_once(JL_ROOT.'/jl_tools.cls.php');

class AdminDo extends JL_controller {
	
	public function __init(){
		$this->page = JL_factory::get_JL_page();
		
		
	}
	
	public function do__(){
		
//		$this->page->set_error('error','一个错误测试');
//		$this->page->display('/user/error.tpl.php');
		
		
		$this->do__index();	
		
		
		
	}

	public function do__index(){
		
		$this->page->display('/zenboss/admin/admin_index.tpl.php');
	

	}

	//文章管理
	public function do__article_manage(){
		
		$table = JL_factory::get_JL_mysql_table('zb_article');
		$table->get_rows('*',' ORDER BY article_add_time DESC');
		$res = $table->exec();
		$article_list = $table->fetch($res);
		$this->page->set_item('article_list',$article_list);
		$this->page->display('/zenboss/admin/article_list.tpl.php');


	}


	//修改文章
	public function do__edit_article(){

		$art_id = $_REQUEST['art_id'];

		$table = JL_factory::get_JL_mysql_table('zb_article');
		
		$table -> get_rows('*',' WHERE article_id='.$art_id);
		
		$res = $table -> exec();
//		var_dump($res);exit;
		$article = mysql_fetch_assoc($res);

		$this->page->set_item('default_content',$article['article_content']);
		
		$this->page->set_item('default_title',$article['article_title']);

		$this->page->set_item('article',$article);

		$this->page->display('/zenboss/admin/edit_article.tpl.php');

	}
	//修改文章 动作
	public function do__edit_article_do(){
		$art_id = $_REQUEST['art_id'];
		
		$title = $_REQUEST['title'];
		$edit_content = $_REQUEST['editor1'];		
		$errors = null;
		if(empty($art_id)){
			$errors['article_id']='文章ID不能为空';
		}
		if(trim($title)==''){
			$errors['article_title'] = '文章标题不能为空';
		}

		if($errors!=null){
			foreach($errors as $error){
				echo $error.'<br />';
			}
			exit;
		}
		
		$data_arr = array(
				'article_title'=>$title,
				'article_content'=>$edit_content,

				);


		$table = JL_factory::get_JL_mysql_table('zb_article');

		$table -> set_rows($data_arr,' article_id = '.$art_id,false);
		$table->exec();

		header('location:'.$_SERVER['PHP_SELF'].'?do=edit_article&art_id='.$art_id);

	}

	//创建新文章
	public function do__new_article(){
		$this->page->set_item('default_content','本站编辑：admin');	
		$this->page->display('/zenboss/admin/new_article.tpl.php');
		
	}
	

	//建立新文章 动作
	public function do__new_article_do(){
		
		$edit_content = $_REQUEST['editor1'];
		$title = $_REQUEST['title'];
		$errors = null;
		if(trim($title)==''){
			
			$errors['title']='标题太短';

		}
		

		if($errors!=null){

			foreach($errors as $error){

				echo $error.'<br />';
			}
			exit;
		}

		$data_arr=array(
			'article_add_time'     =>  time(),
			'article_open_count'   =>  0,
			'article_title'        =>  $title,
			'article_content'      =>  $edit_content,
			);

		$table = JL_factory::get_JL_mysql_table('zb_article');
		
		$table -> new_rows($data_arr);
		$table -> exec();

		header('location:/zb.php');
//		var_dump($_REQUEST);exit;


	}

	//页面管理
	public function do__page_manage(){
		$table = JL_factory::get_JL_mysql_table('zb_page');
		$table -> get_rows('*',' ORDER BY page_add_time DESC');
		$res = $table->exec();
		

	}
	
	public function do__user_reg(){
		$uname = trim($_REQUEST['u']);
		$pwd = $_REQUEST['p'];
		$pwd2 = $_REQUEST['p2'];
		$email = $_REQUEST['email'];
		$errors = null;
		
		
		if(strlen(trim($uname))<6){
			$errors .= '用户名不能少于6个字符<br />';
		}
		if($this->func_get_user_info(trim($uname))){
			$errors .= '用户名已经存在<br />';
		}
		if(strlen($pwd)<6){
			$errors .= '密码不能少于6个字符<br />';
		}
		if($pwd!==$pwd2){
			$errors .= '两次密码输入不一致<br />';
			
		}
		if(strlen($email)<1){
			$errors .= 'email不能为空<br />';
		}
		if(!empty($errors)){
			$this->page->set_error('error',$errors);
			$this->page->display('/user/error.tpl.php');exit;
		}
		
		
		$a = array(
						'user_login_name'        => $uname,
						'user_login_password'    => md5($pwd),
						'user_login_email'       => $email,
							);
		
		$table = JL_factory::get_JL_mysql_table('zb_user');
		$table->new_rows($a);
		
		$table->exec();
		
		$this->page->set_item('message','注册成功');
		$this->page->set_item('go_url','/');
		$this->page->display('/user/message.tpl.php');exit;
		
		
		
		
		
	}
	
	
	public function do__login(){
		$uname = $_REQUEST['u'];
		$pwd = $_REQUEST['p'];
		$url = $_REQUEST['url'];
		if($this->func_login($uname,$pwd)){
			
			
			if(!empty($url)){
				header('location:'.$url);exit;
			}else{
				echo '显示消息:登陆成功';
				
			}
			
		}else{
			echo '提示错误:密码错误';
		}
		
	}
	
	//登陆方法
	public function func_login($uname,$pwd){
		$u_info = $this->func_get_user_info($uname);
		
		if($u_info['user_login_password']==md5($pwd)){
			JL_tools::set_session('login_user',$u_info);

			return true;
		}else{
			
			return false;
		}
	}
	
	//得到用户信息
	public function func_get_user_info($uname){
		
		$table = JL_factory::get_JL_mysql_table('zb_user');
		$table->get_rows('*',"WHERE user_login_name='{$uname}'");
		
		return mysql_fetch_assoc($table->exec());
		
		

		
	}
	
	
	
	
}















?>
